﻿// Decompiled with JetBrains decompiler
// Type: TaleWorlds.MountAndBlade.View.BorderFlagEntityFactory
// Assembly: TaleWorlds.MountAndBlade.View, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null
// MVID: 61152EB8-513B-4D4A-8AC0-0D1465C6CF6C
// Assembly location: D:\steam\steamapps\common\Mount & Blade II Bannerlord\Modules\Native\bin\Win64_Shipping_Client\TaleWorlds.MountAndBlade.View.dll

using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.Core;
using TaleWorlds.DotNet;
using TaleWorlds.Engine;

#nullable disable
namespace TaleWorlds.MountAndBlade.View
{
  public class BorderFlagEntityFactory : IEntityFactory
  {
    private readonly string _prefabName;
    private GameEntity _cachedFlagEntity;

    public BorderFlagEntityFactory(string prefabName) => this._prefabName = prefabName;

    public GameEntity MakeEntity(params object[] paramObjects)
    {
      Scene scene = Mission.Current.Scene;
      if ((NativeObject) this._cachedFlagEntity == (NativeObject) null)
        this._cachedFlagEntity = GameEntity.Instantiate((Scene) null, this._prefabName, false);
      GameEntity cachedFlagEntity = this._cachedFlagEntity;
      GameEntity gameEntity = GameEntity.CopyFrom(scene, cachedFlagEntity);
      gameEntity.SetMobility(GameEntity.Mobility.dynamic);
      Banner banner = (Banner) (((IEnumerable<object>) paramObjects).FirstOrDefault<object>((Func<object, bool>) (o => o is Banner)) ?? (object) Banner.CreateRandomBanner());
      Mesh firstMesh = gameEntity.GetFirstMesh();
      Material tableauMaterial = firstMesh.GetMaterial().CreateCopy();
      Action<Texture> setAction = (Action<Texture>) (tex => tableauMaterial.SetTexture(Material.MBTextureType.DiffuseMap2, tex));
      banner.GetTableauTextureSmall(setAction);
      firstMesh.SetMaterial(tableauMaterial);
      return gameEntity;
    }
  }
}
